/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carrancao.repositorio;

import carrancao.conexao.HibernateUtil;
import carrancao.entidades.Cliente;
import carrancao.entidades.Endereco;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Lubnnia
 */
public class ClienteRepositorio {

    public ClienteRepositorio() {

    }

    public void cadastrarCliente(Cliente c) {

       Session session = HibernateUtil.getSessionFactory().openSession();

       Transaction trans = session.beginTransaction();

       trans.begin();
       session.save(c);
       trans.commit();
       session.close();

    }

    public void deletarCliente(Cliente c) {

       Session session = HibernateUtil.getSessionFactory().openSession();

       Transaction trans = session.beginTransaction();

       trans.begin();
       session.delete(c);
       trans.commit();
       session.close();

    }

    public void alterarCliente(Cliente c) {

       Session session = null;

       if(HibernateUtil.getSessionFactory().openSession().isConnected()){
            HibernateUtil.getSessionFactory().close();
       }
       session = HibernateUtil.getSessionFactory().openSession();

       Cliente cliente = this.pesquisarClientePorID(c.getIdcliente());

       Endereco endereco = c.getEndereco();

       c.setIdcliente(cliente.getIdcliente());
       Transaction trans = session.beginTransaction();

       trans.begin();
       session.update(c);
       trans.commit();
       session.close();

    }

    public Cliente pesquisarClientePorID(int id) {

      Session session = HibernateUtil.getSessionFactory().openSession();

      Cliente cliente = null;

      SQLQuery query = session.createSQLQuery("SELECT {cliente.*} FROM cliente" +
              " {cliente} WHERE idcliente = '" + id + "' ");

      query.addEntity("cliente", Cliente.class);

      if(query.list().size() > 0) {
          cliente = (Cliente) query.list().get(0);
          ClienteRepositorio.initialize(cliente);
      }

      session.close();

      return cliente;

    }

    public List pesquisarClienteTodos() {

      Session session = HibernateUtil.getSessionFactory().openSession();

      List cliente = null;

      SQLQuery query = session.createSQLQuery("SELECT {cliente.*} FROM cliente" +
              " {cliente}");

      query.addEntity("cliente", Cliente.class);

      cliente = query.list();

      for(Object o: cliente){
          ClienteRepositorio.initialize((Cliente) o);
      }

      session.close();

      return cliente;

    }

    public List pesquisarClientePorNome(String nomeCliente) {

       Session session = HibernateUtil.getSessionFactory().openSession();

       List lista = null;

       Cliente cliente = null;

       SQLQuery query = session.createSQLQuery("SELECT {Cliente.*} FROM cliente" +
               " {Cliente} WHERE nome like '%" + nomeCliente + "%'");

       query.addEntity("Cliente", Cliente.class);

       lista = query.list();

       for(Object o: lista){
           ClienteRepositorio.initialize((Cliente) o);
       }

       session.close();

       return lista;

    }

    private static void initialize(Cliente cliente){
         Hibernate.initialize(cliente);
         Hibernate.initialize(cliente.getEndereco());
         Hibernate.initialize(cliente.getDataCadastro());
         Hibernate.initialize(cliente.getDataNascimento());
    }

}
